#!/bin/bash

# RuoYi-TDesign 项目一键部署脚本
# 适用于阿里云ECS环境 (CentOS 7.9)

echo "开始部署 RuoYi-TDesign 项目..."

# 检查并安装必要工具
echo "检查必要工具..."

# 安装EPEL仓库（如果尚未安装）
if ! rpm -q epel-release &> /dev/null; then
    echo "安装EPEL仓库..."
    sudo yum install -y epel-release
fi

# 安装Maven
if ! command -v mvn &> /dev/null; then
    echo "安装 Maven..."
    sudo yum install -y maven || { echo "Maven 安装失败"; exit 1; }
fi

# 安装Node.js (使用EPEL中的版本，避免GLIBC依赖问题)
if ! command -v node &> /dev/null; then
    echo "安装 Node.js (使用EPEL版本以兼容CentOS 7)..."
    # 在CentOS 7上使用EPEL仓库安装Node.js，避免GLIBC依赖问题
    sudo yum install -y nodejs npm --enablerepo=epel || { echo "Node.js 安装失败"; exit 1; }
    
    # 检查Node.js版本
    NODE_VERSION=$(node --version)
    echo "已安装 Node.js 版本: $NODE_VERSION"
    
    # 如果版本过低（小于12），则尝试升级到Node.js 16
    NODE_MAJOR_VERSION=$(echo $NODE_VERSION | cut -d'.' -f1 | cut -dv -f2)
    if [ "$NODE_MAJOR_VERSION" -lt "12" ]; then
        echo "Node.js版本过低，尝试升级..."
        # 清理可能存在的nodesource仓库配置
        sudo rm -f /etc/yum.repos.d/nodesource*.repo
        
        # 使用n模块升级Node.js
        sudo npm install -g n
        sudo n 16 || echo "Node.js升级失败，使用系统默认版本"
    fi
fi

# 安装Docker
if ! command -v docker &> /dev/null; then
    echo "安装 Docker..."
    curl -fsSL https://get.docker.com -o get-docker.sh
    sudo sh get-docker.sh
    sudo systemctl start docker
    sudo systemctl enable docker
fi

# 安装Docker Compose
if ! command -v docker-compose &> /dev/null; then
    echo "安装 Docker Compose..."
    sudo curl -L "https://github.com/docker/compose/releases/download/v2.20.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
    sudo chmod +x /usr/local/bin/docker-compose
    sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
fi

# 配置Docker镜像加速器
echo "配置Docker镜像加速器..."
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": [
    "https://registry.cn-hangzhou.aliyuncs.com",
    "https://docker.mirrors.ustc.edu.cn",
    "https://hub-mirror.c.163.com"
  ]
}
EOF

# 重启Docker服务
echo "重启Docker服务..."
sudo systemctl daemon-reload
sudo systemctl restart docker

# 构建后端服务镜像
echo "构建后端服务镜像..."
cd ruoyi-admin
mvn clean package -DskipTests || { echo "后端服务构建失败"; exit 1; }
docker build -t ruoyi/ruoyi-server:1.4.0 . || { echo "后端Docker镜像构建失败"; exit 1; }

# 构建监控服务镜像
echo "构建监控服务镜像..."
cd ../ruoyi-monitor-admin
mvn clean package -DskipTests || { echo "监控服务构建失败"; exit 1; }
docker build -t ruoyi/ruoyi-monitor-admin:1.4.0 . || { echo "监控服务Docker镜像构建失败"; exit 1; }

# 构建任务调度服务镜像
echo "构建任务调度服务镜像..."
cd ../ruoyi-snailjob-server
mvn clean package -DskipTests || { echo "任务调度服务构建失败"; exit 1; }
docker build -t ruoyi/ruoyi-snailjob-server:1.4.0 . || { echo "任务调度服务Docker镜像构建失败"; exit 1; }

# 构建前端服务镜像
echo "构建前端服务镜像..."
cd ../ruoyi-ui
npm install || { echo "前端依赖安装失败"; exit 1; }
npm run build || { echo "前端构建失败"; exit 1; }
docker build -t ruoyi/ruoyi-ui:1.4.0 . || { echo "前端Docker镜像构建失败"; exit 1; }

# 返回根目录
cd ..

# 创建必要的目录
echo "创建必要的目录..."
sudo mkdir -p /docker/mysql/data /docker/mysql/conf
sudo mkdir -p /docker/redis/data /docker/redis/conf
sudo mkdir -p /docker/minio/data /docker/minio/config
sudo mkdir -p /docker/nginx/cert /docker/nginx/conf.d /docker/nginx/html /docker/nginx/log
sudo mkdir -p /docker/admin/logs /docker/server2/logs /docker/monitor/logs /docker/snailjob/logs

# 启动所有服务
echo "启动所有服务..."
docker-compose -f docker-compose.yml -f docker-compose.prod.yml up -d

echo "部署完成！"
echo "查看服务状态: docker-compose ps"
echo "查看日志: docker-compose logs -f"